﻿namespace Morus.Infrastructure.EnterpriseLibrary
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Configuration;
    using System.Diagnostics.CodeAnalysis;
    using Microsoft.Practices.Unity;
    using Microsoft.Practices.Unity.Configuration;

    public class UnityDependencyResolver : Disposable, IDependencyResolver
    {
        private bool _disposed = false;
        private readonly IUnityContainer _container;

        #region ctor
        [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope")]
        public UnityDependencyResolver()
            : this(new UnityContainer())
        {
            UnityConfigurationSection configuration = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            configuration.Configure(_container);
        }

        public UnityDependencyResolver(IUnityContainer container)
        {
            Check.Argument.IsNotNull(container, "container");

            _container = container;
        }
        #endregion

        public void Register<T>(T instance)
        {
            Check.Argument.IsNotNull(instance, "instance");

            _container.RegisterInstance(instance);
        }

        public void Inject<T>(T existing)
        {
            Check.Argument.IsNotNull(existing, "existing");

            _container.BuildUp(existing);
        }

        public object Resolve(Type type)
        {
            Check.Argument.IsNotNull(type, "type");

            return _container.Resolve(type);
        }

        public object Resolve(Type type, string name)
        {
            Check.Argument.IsNotNull(type, "type");
            Check.Argument.IsNotEmpty(name, "name");

            return _container.Resolve(type, name);
        }

        public T Resolve<T>()
        {
            return _container.Resolve<T>();
        }

        public T Resolve<T>(string name)
        {
            Check.Argument.IsNotEmpty(name, "name");

            return _container.Resolve<T>(name);
        }

        public IEnumerable<T> ResolveAll<T>()
        {
            IEnumerable<T> namedInstances = _container.ResolveAll<T>();
            T unnamedInstance = default(T);

            try
            {
                unnamedInstance = _container.Resolve<T>();
            }
            catch (ResolutionFailedException)
            {
                //When default instance is missing
            }

            if (Equals(unnamedInstance, default(T)))
            {
                return namedInstances;
            }

            return new ReadOnlyCollection<T>(new List<T>(namedInstances) { unnamedInstance });
        }

        protected override void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    if (_container != null)
                    {
                        _container.Dispose();
                    }
                }
            }

            base.Dispose(disposing);

            _disposed = true;
        }
    }
}
